package com.tianji.learning.mq;

import com.tianji.api.dto.remark.LikedTimesDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.CollUtils;
import com.tianji.learning.domain.po.InteractionReply;
import com.tianji.learning.enums.PointsRecordType;
import com.tianji.learning.mq.message.PointsMessage;
import com.tianji.learning.service.IInteractionReplyService;
import com.tianji.learning.service.IPointsRecordService;
import groovyjarjarantlr4.v4.runtime.misc.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
@RequiredArgsConstructor
public class LikeTimesChangeListener {

    private final IInteractionReplyService replyService;
    private final IPointsRecordService pointsRecordService;

    //修改InteractionReply中的点赞字段
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("qa.learning.learning"),
            exchange = @Exchange(value = MqConstants.Exchange.LIKE_RECORD_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.QA_LIKED_TIMES_KEY
    ))
    public void qaLikes(List<LikedTimesDTO> dto) {
        //原始代码 --- 接收参数 >>>LikedTimesDTO dto <<<=========================
        /*
            if (dto == null || dto.getBizId() == null){
                return;
            }
            Long bizId = dto.getBizId();
            Integer likedTimes = dto.getLikedTimes();
            boolean update = replyService.lambdaUpdate()
                    .eq(InteractionReply::getId, bizId)
                    .set(InteractionReply::getLikedTimes, likedTimes)
                    .update();
            if (!update){
                throw new DbException("点赞数更新失败!!!");
            }
        */

        //=====================================================================
        //代码改造,redis优化 (+ mq消息中间件解耦合 --- 后面有的)
        //=====================================================================

        //改造之后===============================================================
        if (CollUtils.isEmpty(dto)) {
            return;
        }
        List<InteractionReply> replyList = new ArrayList<>();
        for (LikedTimesDTO likedTimesDTO : dto) {
            Long bizId = likedTimesDTO.getBizId();
            Integer likedTimes = likedTimesDTO.getLikedTimes();
            if (bizId == null || likedTimes == null) {
                continue;
            }
            InteractionReply reply = new InteractionReply();
            reply.setId(bizId);
            reply.setLikedTimes(likedTimes);
            replyList.add(reply);
        }
        //更新 reply 表的 likeTimes
        replyService.updateBatchById(replyList);
    }

    //新增签到积分记录
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("sign.queue"),
            exchange = @Exchange(value = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.SIGN_IN
    ))
    public void listenerSign(PointsMessage message) {
        if (message == null) {
            return;
        }
        pointsRecordService.addSign(message, PointsRecordType.SIGN);
    }

    //新增问答积分记录
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("reply.queue"),
            exchange = @Exchange(value = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.WRITE_REPLY
    ))
    public void listenerReply(PointsMessage message) {
        if (message == null) {
            return;
        }
        pointsRecordService.addSign(message, PointsRecordType.QA);
    }

}
